using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEditor;
using UnityEditor.U2D;
using Object = System.Object;

[CustomEditor(typeof(UIBehaviourRoot))]
public class UIBehaviourRootEditor : Editor
{
    private SerializedProperty HandleClass;
    private SerializedProperty EventHandlers;
    private UIBehaviour _behaviour;

    void OnEnable()
    {
        HandleClass = serializedObject.FindProperty("HandleClsName");
        _behaviour = (UIBehaviour)target;
    }

    private void BuildComponent(string name, UIAnchor.Side side)
    {
        var go = new GameObject() {name = name};
        go.transform.parent = _behaviour.transform;
        var anchor = go.AddComponent<UIAnchor>();
        anchor.side = side;
        anchor.uiCamera = GameObject.FindGameObjectWithTag("UICamera").GetComponent<Camera>();
        go.AddComponent<UIBehaviour>();
    }

    public override void OnInspectorGUI()
    {
        serializedObject.Update();
        EditorGUI.BeginDisabledGroup(true);
        EditorGUILayout.PropertyField(HandleClass);
        EditorGUI.EndDisabledGroup();

        //add funtions that create 5 anchors
        if (GUILayout.Button("CenterAnchor"))
        {
            var obj = _behaviour.transform.Find("Center");
            if(obj == null)
            {
                BuildComponent("Center", UIAnchor.Side.Center);
            }
        }

        if (GUILayout.Button("TopAnchor"))
        {
            var obj = _behaviour.transform.Find("Top");
            if(obj == null)
            {
                BuildComponent("Top", UIAnchor.Side.Top);
            }
        }

        if (GUILayout.Button("TopLeftAnchor"))
        {
            var obj = _behaviour.transform.Find("TopLeft");
            if(obj == null)
            {
                BuildComponent("TopLeft", UIAnchor.Side.TopLeft);
            }
        }

        if (GUILayout.Button("TopRightAnchor"))
        {
            var obj = _behaviour.transform.Find("TopRight");
            if(obj == null)
            {
                BuildComponent("TopRight", UIAnchor.Side.TopRight);
            }
        }

        if (GUILayout.Button("LeftAnchor"))
        {
            var obj = _behaviour.transform.Find("Left");
            if(obj == null)
            {
                BuildComponent("Left", UIAnchor.Side.Left);
            }
        }

        if (GUILayout.Button("RightAnchor"))
        {
            var obj = _behaviour.transform.Find("Right");
            if(obj == null)
            {
                BuildComponent("Right", UIAnchor.Side.Right);
            }
        }

        if (GUILayout.Button("BottomAnchor"))
        {
            var obj = _behaviour.transform.Find("Bottom");
            if(obj == null)
            {
                BuildComponent("Bottom", UIAnchor.Side.Bottom);
            }
        }

        if (GUILayout.Button("BottomLeftAnchor"))
        {
            var obj = _behaviour.transform.Find("BottomLeft");
            if(obj == null)
            {
                BuildComponent("BottomLeft", UIAnchor.Side.BottomLeft);
            }
        }

        if (GUILayout.Button("BottomRightAnchor"))
        {
            var obj = _behaviour.transform.Find("BottomRight");
            if(obj == null)
            {
                BuildComponent("BottomRight", UIAnchor.Side.BottomRight);
            }
        }
    }
}
